package org.dept.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dept.mapper.UserMapper;
import org.dept.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wrapper")
public class WrapperController {
    @Resource
    private UserMapper userMapper;

    //查询name不为空的用户，并且邮箱不为空的用户，年龄大于等于12的
    @GetMapping("/list")
    public List<User> list(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.isNotNull("name")
                .isNotNull("email")
                .ge("age",12);
        return userMapper.selectList(wrapper);
    }
    //查询name为Tom的用户
    @GetMapping("/getName")
    public User name(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.eq("name","Tom");
        return userMapper.selectOne(wrapper);
    }
    //查询年龄在1~20之间的人数
    @GetMapping("/getAge")
    public int age(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.between("age",1,20);//区间
        return userMapper.selectCount(wrapper);//查询结果数
    }

    //模糊查询
    @GetMapping("/like")
    public List<Map<String,Object>> like(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.notLike("name","z");
        List<Map<String,Object>> maps =userMapper.selectMaps(wrapper);
        return maps;
    }

    //通过id子查询
    @GetMapping("/inSql")
    public List<Object> inSql(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.inSql("id","select id from user where id<3");
        List<Object> objects = userMapper.selectObjs(wrapper);
        return objects;
    }

    //通过id排序
    @GetMapping("/orderBy")
    public List<User> orderBy(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.orderByDesc("id");
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }
}
